package com.stylefeng.guns.modular.customer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.customer.dao.CustomerCompanyDao;
import com.stylefeng.guns.modular.customer.dao.PayPlanDao;
import com.stylefeng.guns.modular.customer.service.IPayPlanService;
import com.stylefeng.guns.modular.customer.transfer.PayPlanXlsM;
import com.stylefeng.guns.modular.system.dao.PayPlanMapper;
import com.stylefeng.guns.modular.system.model.CustomerCompany;
import com.stylefeng.guns.modular.system.model.PayPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.ValidationException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 付款计划 服务实现类
 * </p>
 *
 * @author yinjc123
 * @since 2018-04-03
 */
@Service
public class PayPlanServiceImpl extends ServiceImpl<PayPlanMapper, PayPlan> implements IPayPlanService {

    @Autowired
    private PayPlanDao payPlanDao;
    @Autowired
    private CustomerCompanyDao customerCompanyDao;

    @Override
    public List<Map<String, Object>> selectPayPlanesPage(Page<PayPlan> page, Integer customerCompanyId, String providerName) {
        return payPlanDao.getPayPlanesPage(page, customerCompanyId, providerName);
    }

    @Transactional
    @Override
    public void insertXlsListPayPlan(List<PayPlanXlsM> xlsMList) {
        for (PayPlanXlsM xlsM : xlsMList) {
            CustomerCompany customerCompany = customerCompanyDao.selectByUnifiedSocialCreditCode(xlsM.getUnifiedSocialCreditCode());
            if (customerCompany == null) {
                throw new ValidationException(xlsM.getCustomerCompanyName() + "不存在,统一社会信用代码为:" + xlsM.getUnifiedSocialCreditCode());
            }
            xlsM.setCustomerCompanyId(customerCompany.getId());
        }
        for (PayPlanXlsM xlsM : xlsMList) {
            PayPlan payPlan = new PayPlan();
            BeanKit.copyProperties(xlsM, payPlan);
            payPlan.setPayAmount(strChangeBigDecimal(xlsM.getPayAmount()));
            payPlan.setCrash(strChangeBigDecimal(xlsM.getCrash()));
            payPlan.setSilverTicket(strChangeBigDecimal(xlsM.getSilverTicket()));
            payPlan.setBusinessTicket(strChangeBigDecimal(xlsM.getBusinessTicket()));
            payPlan.setHangxin(strChangeBigDecimal(xlsM.getHangxin()));
            if (xlsM.getSilverTicketPeriod() == null || xlsM.getSilverTicketPeriod() == "") {
                payPlan.setSilverTicketPeriod(new Integer(0));
            } else {
                payPlan.setSilverTicketPeriod(new Integer(xlsM.getSilverTicketPeriod()));
            }
            if (xlsM.getBusinessTicketPeriod() == null || xlsM.getBusinessTicketPeriod() == "") {
                payPlan.setBusinessTicketPeriod(new Integer(0));
            } else {
                payPlan.setBusinessTicketPeriod(new Integer(xlsM.getBusinessTicketPeriod()));
            }
            if (xlsM.getHangxinPeriod() == null || xlsM.getHangxinPeriod() == "") {
                  payPlan.setHangxinPeriod(new Integer(0));
            } else {
                  payPlan.setHangxinPeriod(new Integer(xlsM.getHangxinPeriod()));
            }
            super.insert(payPlan);
        }
    }

    private BigDecimal strChangeBigDecimal(String string) {
        if (string == null || string == "") {
            return new BigDecimal(0);
        }
        return new BigDecimal(string);

    }

    @Override
    public void deleteByCustomerCompanyId(Integer customerCompanyId) {
        EntityWrapper<PayPlan> ew = new EntityWrapper<PayPlan>();
        ew.eq("customer_company_id", customerCompanyId);
        delete(ew);
    }

}
